package com.tenglong.teacher.ui.login;

import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.auth.LoginInfo;
import com.netease.nimlib.sdk.uinfo.model.UserInfo;
import com.tenglong.teacher.data.resultInfo.ClientUserInfo;
import com.tenglong.teacher.data.resultInfo.DataState;
import com.tenglong.teacher.data.resultInfo.base.BaseResult;
import com.tenglong.teacher.data.resultInfo.base.ResultVerifier;
import com.tenglong.teacher.services.hdx.CreateRequestEntity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author hdx on 2019/5/31.
 */
public class WYLogin {
    private HunXinLoginListener hunXinLoginListener;
    private HunXinListener listener;
    private int cycleIndex = 0;

    public WYLogin(final HunXinListener listener) {
        this.listener = listener;
        this.hunXinLoginListener = new HunXinLoginListener() {
            @Override
            public void onSucceed() {
                if (listener != null) {
                    listener.onSucceed();
                }
            }

            @Override
            public void onFailed() {
                if (cycleIndex < 1) {
                    resetWYParam();
                } else {
                    cycleIndex = 0;
                    if (listener != null) {
                        listener.onFailed();
                    }
                }
            }
        };
    }

    public void loginNim() {
        ClientUserInfo userInfo = DataState.getInstance().getUserInfo();
        NimUIKit.login(new LoginInfo(userInfo.getImUserName(),
                userInfo.getImUserPassword()), new RequestCallback<LoginInfo>() {
            @Override
            public void onSuccess(LoginInfo param) {
                if (hunXinLoginListener != null) {
                    hunXinLoginListener.onSucceed();
                }
            }

            @Override
            public void onFailed(int code) {
                if (code == 302) {
                    if (hunXinLoginListener != null) {
                        hunXinLoginListener.onFailed();
                    }
                } else {
                    if (listener != null) {
                        listener.onFailed();
                    }
                }
            }

            @Override
            public void onException(Throwable exception) {
                ResultVerifier.systerErrers(exception);
                if (listener != null) {
                    listener.onFailed();
                }
            }
        });
    }


    private void resetWYParam() {
        CreateRequestEntity.getWebService().resetTicket().enqueue(new Callback<BaseResult<ClientUserInfo>>() {
            @Override
            public void onResponse(Call<BaseResult<ClientUserInfo>> call, final Response<BaseResult<ClientUserInfo>> response) {
                if (ResultVerifier.isSucceed(response)) {
                    cycleIndex += 1;
                    String imUserName = response.body().data.getImUserName();
                    String imUserPassword = response.body().data.getImUserPassword();
                    ClientUserInfo userInfo = DataState.getInstance().getUserInfo();
                    userInfo.setImUserName(imUserName);
                    userInfo.setImUserPassword(imUserPassword);
                    if (listener != null) {
                        listener.onChangeParams();
                    }
                    loginNim();
                } else {
                    if (listener != null) {
                        listener.onFailed();
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseResult<ClientUserInfo>> call, Throwable t) {
                ResultVerifier.systerErrers(t);
                if (listener != null) {
                    listener.onFailed();
                }
            }
        });
    }


    public interface HunXinListener {
        void onSucceed();

        void onFailed();

        void onChangeParams();

    }


    public interface HunXinLoginListener {
        void onSucceed();

        void onFailed();
    }
}
